﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.common.unit;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.BillDictionary;
using EkStudio.com.bean.xml.BillModule;

namespace EkStudio.com.ui.form
{
    /// <summary>
    /// 字段选择器
    /// </summary>
    public partial class ModuleFieldSelectForm : DevExpress.XtraEditors.XtraForm
    {

        public enum ModuleLoadType
        {
            LoadTypeAll,//加载所有
            LoadTypeMain,//只加载主表
            LoadTypeSubject,//只加载子表
        };

        private BillUiFieldsItem selectField = null;

        private BillUiFieldsItem[] selectFields = null;

        private List<string> selectPaths = new List<string>();

        private BillFormatModule format = null;

        /// <summary>
        /// 字段选择器
        /// </summary>
        public ModuleFieldSelectForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 加载模块下的所有字段组
        /// </summary>
        private void InitForm()
        {
            RootTopPanel.Text = string.Format("【{0}】【模块字段选择器】", ProjectUtil.PROJECT_NAME);
        }
        /*
        /// <summary>
        /// 设置多模块选择
        /// </summary>
        /// <param name="format"></param>
        public void SetFormatBill(BillFormatModule[] format)
        {
            TreeViewMenu.Nodes.Clear();
            ListViewContent.Items.Clear();

            TreeNode Root = new TreeNode();
            Root.Text = "模块列表";
            Root.ImageIndex = 2;
            Root.SelectedImageIndex = 2;

            if (format!=null && format.Length > 0)
            {
                for(int i = 0; i < format.Length; i++)
                {
                    BillFormatModule mdoule = format[i];
                    if(mdoule is BillFormatModule)
                    {
                        BillFormatModule item = (BillFormatModule)mdoule;
                        TreeNode RootNode = new TreeNode();
                        RootNode.Text = item.getCaption();
                        RootNode.ImageIndex = 0;
                        RootNode.SelectedImageIndex = 0;
                        if (item.UiField != null && item.UiField.FieldsProperties != null
                            && item.UiField.FieldsProperties.Count > 0)
                        {
                            for (int k = 0; k < item.UiField.FieldsProperties.Count; k++)
                            {
                                BillUiGroupItems group = item.UiField.FieldsProperties[k];
                                TreeNode node = new TreeNode();
                                node.ImageIndex = 1;
                                node.SelectedImageIndex = 1;
                                node.Text = group.Caption;
                                node.Tag = group;
                                RootNode.Nodes.Add(node);
                            }
                        }

                        Root.Nodes.Add(RootNode);
                    }
                    else //字典类型 
                    {

                    }
                }
            }

            TreeViewMenu.Nodes.Add(Root);
            TreeViewMenu.ExpandAll();
        }

        */
        /// <summary>
        /// 设置单模块选择
        /// </summary>
        /// <param name="FormatBill"></param>
        public void SetFormatBill(BillFormatModule FormatBill)
        {
            format = FormatBill;
            TreeViewMenu.Nodes.Clear();
            ListViewContent.Items.Clear();

            TreeNode Root = new TreeNode();
            Root.Text = "模块列表";
            Root.ImageIndex = 2;
            Root.SelectedImageIndex = 2;

            if (FormatBill.UiField!=null)
            {
                TreeNode RootNode = new TreeNode();
                RootNode.Text = FormatBill.getCaption();
                RootNode.ImageIndex = 0;
                RootNode.SelectedImageIndex = 0;
                RootNode.Tag = FormatBill;
                if (FormatBill.UiField != null && FormatBill.UiField.FieldsProperties != null
                    && FormatBill.UiField.FieldsProperties.Count > 0)
                {
                    for (int i = 0; i < FormatBill.UiField.FieldsProperties.Count; i++)
                    {
                        BillUiGroupItems group = FormatBill.UiField.FieldsProperties[i];
                        TreeNode node = new TreeNode();
                        node.ImageIndex = 1;
                        node.SelectedImageIndex = 1;
                        node.Text = group.Caption;
                        node.Tag = group;
                        RootNode.Nodes.Add(node);
                    }
                }

                Root.Nodes.Add(RootNode);
            }
            else if(FormatBill.getBillType() == ProjectUtil.TYPE_DIC_MODULE)
            {
                BillDicModuleItem dic = FormatBill as BillDicModuleItem;
                if (dic != null)
                {
                    TreeNode RootNode = new TreeNode();
                    RootNode.Text = FormatBill.getCaption();
                    RootNode.ImageIndex = 0;
                    RootNode.SelectedImageIndex = 0;
                    RootNode.Tag = FormatBill;
                    Root.Nodes.Add(RootNode);
                }
            }
            
            TreeViewMenu.Nodes.Add(Root);
            TreeViewMenu.ExpandAll();
        }
        /// <summary>
        /// 选择性加载模块下的字段组
        /// </summary>
        /// <param name="FormatBill"></param>
        /// <param name="loadType"></param>
        public void SetFormatBill(BillFormatModule FormatBill, ModuleLoadType loadType)
        {
            format = FormatBill;
            TreeViewMenu.Nodes.Clear();
            ListViewContent.Items.Clear();

            TreeNode BaseRoot = new TreeNode();
            BaseRoot.Text = "模块列表";
            BaseRoot.ImageIndex = 2;
            BaseRoot.SelectedImageIndex = 2;
            //当前是模块属性
            if (FormatBill.UiField != null) 
            {
                TreeNode root = new TreeNode();
                root.Text = FormatBill.getCaption();
                root.ImageIndex = 0;
                root.SelectedImageIndex = 0;
                root.Tag = FormatBill;

                if (FormatBill.UiField != null && FormatBill.UiField.FieldsProperties != null
                    && FormatBill.UiField.FieldsProperties.Count > 0)
                {
                    for (int i = 0; i < FormatBill.UiField.FieldsProperties.Count; i++)
                    {
                        BillUiGroupItems group = FormatBill.UiField.FieldsProperties[i];
                        if (loadType == ModuleLoadType.LoadTypeAll)
                        {
                            TreeNode node = new TreeNode();
                            node.ImageIndex = 1;
                            node.SelectedImageIndex = 1;
                            node.Text = group.Caption;
                            node.Tag = group;
                            root.Nodes.Add(node);
                        }
                        else if (loadType == ModuleLoadType.LoadTypeMain)
                        {
                            if (!group.Table)
                            {
                                TreeNode node = new TreeNode();
                                node.ImageIndex = 1;
                                node.SelectedImageIndex = 1;
                                node.Text = group.Caption;
                                node.Tag = group;
                                root.Nodes.Add(node);
                            }
                        }
                        else if (loadType == ModuleLoadType.LoadTypeSubject)
                        {
                            if (group.Table)
                            {
                                TreeNode node = new TreeNode();
                                node.ImageIndex = 1;
                                node.SelectedImageIndex = 1;
                                node.Text = group.Caption;
                                node.Tag = group;
                                root.Nodes.Add(node);
                            }
                        }
                    }
                }
                BaseRoot.Nodes.Add(root);
                
            }
            TreeViewMenu.Nodes.Add(BaseRoot);
            TreeViewMenu.ExpandAll();
        }


        /// <summary>
        /// 只加载字段组名为tableKey的字段组
        /// </summary>
        /// <param name="FormatBill"></param>
        /// <param name="tableKey"></param>
        public void SetFormatBill(BillFormatModule FormatBill, string tableKey)
        {
            format = FormatBill;
            TreeViewMenu.Nodes.Clear();
            ListViewContent.Items.Clear();
            TreeNode BaseRoot = new TreeNode();
            BaseRoot.Text = "模块列表";
            BaseRoot.ImageIndex = 2;
            BaseRoot.SelectedImageIndex = 2;
            //当前是模块属性
            if (FormatBill.UiField!=null)
            {
                TreeNode root = new TreeNode();
                root.Text = FormatBill.getCaption();
                root.ImageIndex = 0;
                root.SelectedImageIndex = 0;
                root.Tag = FormatBill;


                if (FormatBill.UiField != null && FormatBill.UiField.FieldsProperties != null
                    && FormatBill.UiField.FieldsProperties.Count > 0)
                {
                    for (int i = 0; i < FormatBill.UiField.FieldsProperties.Count; i++)
                    {
                        BillUiGroupItems group = FormatBill.UiField.FieldsProperties[i];
                        if (group.Key == tableKey)
                        {
                            TreeNode node = new TreeNode();
                            node.ImageIndex = 1;
                            node.SelectedImageIndex = 1;
                            node.Text = group.Caption;
                            node.Tag = group;
                            root.Nodes.Add(node);
                        }
                        else if (!group.Table)
                        {
                            TreeNode node = new TreeNode();
                            node.ImageIndex = 1;
                            node.SelectedImageIndex = 1;
                            node.Text = group.Caption;
                            node.Tag = group;
                            root.Nodes.Add(node);
                        }

                    }
                }
                BaseRoot.Nodes.Add(root);
                
            }
            TreeViewMenu.Nodes.Add(BaseRoot);
            TreeViewMenu.ExpandAll();
        }

        public BillUiFieldsItem SelectField
        {
            get
            {
                return selectField;
            }

            set
            {
                selectField = value;
            }
        }

        public BillUiFieldsItem[] SelectFields
        {
            get { return selectFields; }
            set { selectFields = value; }
        }

        public List<string> SelectPaths
        {
            get
            {
                return selectPaths;
            }

            set
            {
                selectPaths = value;
            }
        }

        private void TreeViewMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeViewMenu.SelectedNode = e.Node;
            TreeNode select = TreeViewMenu.SelectedNode;
            if (select != null)
            {
                ListViewContent.Items.Clear();
                if (select.Tag is BillUiGroupItems)
                {
                    BillUiGroupItems group = (BillUiGroupItems)select.Tag;
                    if (group.Fields != null && group.Fields.Count > 0)
                    {
                        for (int i = 0; i < group.Fields.Count; i++)
                        {
                            BillUiFieldsItem field = group.Fields[i];
                            ListViewItem ViewIt = new ListViewItem(field.Caption, 0);
                            ViewIt.Tag = field;
                            ListViewContent.Items.Add(ViewIt);
                        }
                    }
                }

            }
        }


        private void ListViewContent_Click(object sender, EventArgs e)
        {
            SelectPaths.Clear();
            if (ListViewContent.SelectedItems.Count > 0)
            {
                selectField = (BillUiFieldsItem)ListViewContent.SelectedItems[0].Tag;
                selectFields = new BillUiFieldsItem[ListViewContent.SelectedItems.Count];

                for (int i = 0; i < ListViewContent.SelectedItems.Count; i++)
                {
                    StringBuilder path = new StringBuilder();
                    selectFields[i] = (BillUiFieldsItem)ListViewContent.SelectedItems[i].Tag;
                    
                    //获取当前树节点
                    TreeNode select = TreeViewMenu.SelectedNode;
                    
                    TreeNode parent =  select.Parent;
                    if (parent != null)
                    {
                        if(parent.Tag is BillFormatModule)
                        {
                            BillFormatModule item = (BillFormatModule)parent.Tag;
                            path.Append(item.getCaption());
                        }
                    }
                    else
                    {
                        MessageBox.Show("获取父级节点失败");
                    }
                    if (select.Tag is BillUiGroupItems)//当前是模块分组
                    {
                        BillUiGroupItems group = (BillUiGroupItems)select.Tag;
                        if (path.Length > 0) path.Append(".");
                        path.Append(group.Caption);
                    }
                    else if (select.Tag is BillDicModuleItem)//如果当前是字典时
                    {

                    }
                    if (path.Length > 0) path.Append(".");
                    path.Append(selectFields[i].Caption);
                    SelectPaths.Add(path.ToString());
                }
            }
            else
            {
                selectField = null;
                selectFields = null;
            }
        }

        private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置

        private void ModuleFieldSelectForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                FormLocation = this.Location;
                mouseOffset = Control.MousePosition;
            }
        }

        private void ModuleFieldSelectForm_MouseMove(object sender, MouseEventArgs e)
        {
            int _x = 0;
            int _y = 0;
            if (isMouseDown)
            {
                Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;

                this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
            }
        }

        private void ModuleFieldSelectForm_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {

        }
    }
}